﻿using ServiceCore.Tool.MongoDB;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static ServiceCore.Tool.Mqtt.EMQ_Helper;
using Sensor_Entity;
using MongoDB.Bson;
using MongoDB.Driver;

namespace ServiceCore.Tool.Mqtt
{

    public interface IEmqMongo_Service
    {
        void saveConnected(connectedClass Item);
        void saveDisConnected(disconnectedClass Item);
        List<ResultClientInfomation> getClientInfomation(string sn);
        void SaveSubdata(product_sensor_value model);
    }
    /// <summary>
    /// 用于控制连接MQTT时的
    /// </summary>
    public class EmqMongo_Service : IEmqMongo_Service
    {

        private readonly IMongoDB_Client<connectedClass> _connectedService;
        private readonly IMongoDB_Client<disconnectedClass> _disconnectedService; 
        private readonly IMongoDB_Client<product_sensor_value> _dataService;
        public EmqMongo_Service(
            IMongoDB_Client<connectedClass> connectedService,
            IMongoDB_Client<disconnectedClass> disconnectedService,
            IMongoDB_Client<product_sensor_value> dataService
            )
        {
            _connectedService = connectedService; 
            _disconnectedService = disconnectedService;   
            _dataService = dataService;
        }
        /// <summary>
        /// 新增一条上线信息
        /// </summary>
        /// <param name="Item"></param>
        public void saveConnected(connectedClass Item)
        {
            _connectedService.InsertItem(Item);
        }
        /// <summary>
        /// 新增一条下线信息
        /// </summary>
        /// <param name="Item"></param>
        public void saveDisConnected(disconnectedClass Item)
        {
            _disconnectedService.InsertItem(Item);
        } 
        /// <summary>
        /// 新增一条传感器上传的数据
        /// </summary>
        /// <param name="model"></param>
        public void SaveSubdata(product_sensor_value model)
        {
            _dataService.InsertItem(model);
        } 
        /// <summary>
        /// 获取指定SN的上下线记录
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public List<ResultClientInfomation> getClientInfomation(string sn)
        {
            var connect = _connectedService.Linq_Search(s => s.info.sn == sn, s => s.addtime);
            var disconnect = _disconnectedService.Linq_Search(s => s.info.sn == sn, s => s.addtime);
            var q = from o in connect
                    join p in disconnect on o.clientid equals p.clientid into o_p
                    from p in o_p.DefaultIfEmpty()
                    select new ResultClientInfomation()
                    {
                        connected = o,
                        disconnected = p
                    };
            return q.ToList();
        }

    }
    /// <summary>
    /// 连接的列表
    /// </summary>
    public class ResultClientInfomation
    { 
        /// <summary>
        /// 一个上线信息
        /// </summary>
        public connectedClass connected { get; set; }
        /// <summary>
        /// 一个下线信息
        /// </summary>

        public disconnectedClass disconnected { get; set; }
    }
}
